package com.github.bridge.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.github.bridge.domain.SysConfig;
import com.github.bridge.service.WeightConfigService;
import com.github.bridge.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: signature-bridge-service
 * @description:
 * @author: hui.zhang
 * @date: 2024/9/8 6:34 下午
 **/

@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Tag(name = "认证")
@ApiSupport(order = 3, author = "张辉")
public class AuthController {
    private final WeightConfigService weightConfigService;

    @SaIgnore
    @Operation(summary = "获取token")
    @RequestMapping(value = "/accessToken", method = {RequestMethod.GET, RequestMethod.POST})
    public R<SaTokenInfo> accessToken(@RequestParam("systemId") String systemId,
                                      @RequestParam("secret") String secret) {
        SysConfig sysConfig = weightConfigService.findBySystemId(systemId);
        if (sysConfig == null) {
            throw new RuntimeException("账号密码错误");
        }
        if (sysConfig.getSecret().equals(secret)) {
            StpUtil.login(systemId);
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return R.ok(tokenInfo);
        }
        throw new RuntimeException("验证失败");
    }
}
